<?php

class DeleteImageAction extends CAction
{
    public function run()
    {
        if(!Yii::app()->request->isPostRequest || !isset($_GET['id']))
            throw new CHttpException(400,'the request is invalid');
        
        $recipe=Recipe::loadObjectById($_GET['id'],array('userId','image'));
        if($recipe===null)
            throw new CHttpException(404,'The requested recipe does not exist.');
        if($recipe->userId!=Yii::app()->user->id)
            throw new CHttpException(403,'cannot edit the recipe not belong to you');

        $succ=false;
        if(!$recipe->image){
            $succ=true;        
        }
        else {   
            $image = $recipe->image;
            $recipe->image = UserImage::$null;
            if(Recipe::saveObject($recipe, array('image','updateTime'))) {
                UserImage::delete('recipe',$image);
                $succ=true;
            }
        }

        $this->controller->renderPartial('deleteImage',array('succ'=>$succ, 'recipe'=>$recipe), false, true);
    }
}
  
?>